﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Infrastructure;

namespace Enterprise.UI.Web.Modules.ZuanjingRb
{
    public partial class ZjrbManage : System.Web.UI.Page
    {

        public string zjrq = (string)Enterprise.Infrastructure.Utility.sink("zjrq", Infrastructure.Utility.MethodType.Get, 0, 0, Infrastructure.Utility.DataType.Str);
        public string jh = (string)Enterprise.Infrastructure.Utility.sink("zjjh", Infrastructure.Utility.MethodType.Get, 0, 0, Infrastructure.Utility.DataType.Str); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDL();
                InitData();                
                BindGrid();
            }
        }

        private void InitDDL()
        {
            Enterprise.Service.ZJRB.ZJRBService service = new Service.ZJRB.ZJRBService();
            ddl_rq.DataSource = service.getRiQi();
            ddl_rq.DataTextField = "zjrq";
            ddl_rq.DataValueField = "zjrq";
            ddl_rq.DataBind();
        }

        private void BindGrid()
        {
            Enterprise.Service.ZJRB.ZJRBService service = new Service.ZJRB.ZJRBService();
            gv.DataSource = service.GetList("ZJRQ ='"+ddl_rq.SelectedValue+"'");
            gv.DataBind();
        }

        private void InitData()
        {
            Enterprise.Model.ZJRB.ZJRBEntity model = new Model.ZJRB.ZJRBEntity();
            Enterprise.Service.ZJRB.ZJRBService service = new Service.ZJRB.ZJRBService();
            model = service.GetModel(zjrq, jh);
            if (model!= null)
            {
                hzjjh.Value = model.JH;
                hzjrq.Value = model.ZJRQ;
                tb_rq.Text = model.ZJRQ;
                tb_jh.Text = model.JH;
                tb_rb.Text = model.ZJRB;
                tb_jc.Text = model.ZJJC.ToString();
                tb_bz.Text = model.BZSM;
                if (model.ZJZT == "")
                    ddl_zt.SelectedValue = "1";
                else
                    ddl_zt.SelectedValue = model.ZJZT;
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            Enterprise.Model.ZJRB.ZJRBEntity model = new Model.ZJRB.ZJRBEntity();
            Enterprise.Service.ZJRB.ZJRBService service = new Service.ZJRB.ZJRBService();
            
            if (!string.IsNullOrEmpty(hzjrq.Value) && !string.IsNullOrEmpty(hzjjh.Value))
            {
                model=service.GetModel(hzjrq.Value,hzjjh.Value);
            }            
            model.ZJJC = tb_jc.Text.ToDecimal();
            model.ZJRB = tb_rb.Text;
            model.BZSM = tb_bz.Text;
            model.ZJZT = ddl_zt.SelectedValue;
            if (model.ZJRQ != "" && model.JH != "")
            {
                service.Update(model);
            }
            else
            {
                model.ZJRQ = tb_rq.Text;
                model.JH = tb_jh.Text;
                service.Add(model);
            }
            Response.Redirect("zjrbmanage.aspx", true);
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gv_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("zjrbmanage.aspx?zjrq="+gv.SelectedRow.Cells[0].Text+"&zjjh="+gv.SelectedRow.Cells[1].Text,true);
        }
    }
}